/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tkgarment.system.action;

import com.tkgarment.system.dao.AddressDAO;
import com.tkgarment.system.dao.BloodTypeDAO;
import com.tkgarment.system.dao.DepartmentDAO;
import com.tkgarment.system.dao.EmpTypeDAO;
import com.tkgarment.system.dao.EmployeeDAO;
import com.tkgarment.system.dao.GraduationDAO;
import com.tkgarment.system.dao.MaritalDAO;
import com.tkgarment.system.dao.ProvinceDAO;
import com.tkgarment.system.dao.ReligionDAO;
import com.tkgarment.system.dao.SexTypeDAO;
import com.tkgarment.system.dao.TitleDAO;
import com.tkgarment.system.db.Database;
import com.tkgarment.system.domain.Address;
import com.tkgarment.system.domain.BloodType;
import com.tkgarment.system.domain.Department;
import com.tkgarment.system.domain.Employee;
import com.tkgarment.system.domain.EmployeeType;
import com.tkgarment.system.domain.Graduation;
import com.tkgarment.system.domain.MaritalStatus;
import com.tkgarment.system.domain.Province;
import com.tkgarment.system.domain.Religion;
import com.tkgarment.system.domain.SexType;
import com.tkgarment.system.domain.TitleName;
import com.tkgarment.system.factory.DAOFactory;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author zraj
 */
public class EmployeeAction extends ActionSupportBase implements SessionAware {

    Map<String,Object> session;
    List<TitleName> listTitle;
    List<SexType> listSexType;
    List<Religion> listReligion;
    List<BloodType> listBloodType;
    List<Province> listProvince;
    List<MaritalStatus> listMarital;
    List<Graduation> listGraduation;
    List<Department> listDepartment;
    List<EmployeeType> listEmpType;
    List<Employee> emps;
    String empID;
    Employee emp;
    Religion religion;
    TitleName titleName;
    SexType sexType;
    Province birthProvince,addProvince;
    BloodType bloodType;
    MaritalStatus marital;
    Graduation graduate;
    Department dept;
    EmployeeType empType;
    Address add;
    String oldEmpId;
    Integer activeTab;
    String searchDept;
    
    
    public String deleteEmp() throws Exception {
        
        EmployeeDAO eDao = new EmployeeDAO();
        emp.setEmpId(oldEmpId);
        int i = eDao.deleteSingle(emp);
        if (i == 1 ) {
              setMessage("ลบข้อมูลเรียบร้อยแล้ว");
         return "success";    
        }else {
              setMessage("เกิดข้อผิดพลาด ไม่สามารถลบข้อมูลได้");
         return "success";   
        }
        
        
    }
    
    
    public String editEmp() throws Exception {
         try {
           // System.out.println("Emp:" + emp.getEmpId());
            empID = emp.getEmpId();
           
            add.setEmpID(emp.getEmpId());
            
             Calendar cal = Calendar.getInstance(Locale.US);
            
            
            if (emp.getBirthDate()!= null) {
               cal.setTime(emp.getBirthDate());
               cal.add(Calendar.YEAR, 543);
               emp.setBirthDate(cal.getTime());
               emp.setBirthDate(new java.sql.Date(emp.getBirthDate().getTime()));    
            }
            if (emp.getIdcardEndDate() != null) {
                cal.setTime(emp.getIdcardEndDate());
                cal.add(Calendar.YEAR, 543);
                emp.setIdcardEndDate(new java.sql.Date(cal.getTime().getTime()));
            }
            if (emp.getIdcardStartDate() != null) {
                cal.setTime(emp.getIdcardStartDate());
                cal.add(Calendar.YEAR, 543);
                emp.setIdcardStartDate(new java.sql.Date(cal.getTime().getTime()));
            }
            if (emp.getWorkStartDate() != null) {
                cal.setTime(emp.getWorkStartDate());
                cal.add(Calendar.YEAR, 543);
                emp.setWorkStartDate(new java.sql.Date(cal.getTimeInMillis()));
            }
            
            if (emp.getWorkEndDate()!= null) {
                cal.setTime(emp.getWorkEndDate());
                cal.add(Calendar.YEAR, 543);
                emp.setWorkEndDate(new java.sql.Date(cal.getTimeInMillis()));
            }
            
            
            if (emp.getWorkRegisDate() != null) {
                cal.setTime(emp.getWorkRegisDate());
                cal.add(Calendar.YEAR, 543);
                emp.setWorkRegisDate(new java.sql.Date(cal.getTime().getTime()));
            }
            
            // System.out.println("empID :" + emp.getEmpId());
             Database con = DAOFactory.creatConnection();
        con.beginTransaction();
        EmployeeDAO empDao = DAOFactory.createEmployeeDao();
        empDao.setCon(con);
        int i = empDao.updateSingle(emp,oldEmpId);
        System.out.println("add emp :" + i);
        if (i == 1) {
            AddressDAO addDao = DAOFactory.createAddressDao();
            
            addDao.setCon(con);
            i = addDao.updateSingle(add,add.getAddressID());
            System.out.println("add add :" + i);
            if (i == 1) {
                con.commit();
                setMessage("แก้ไขข้อมูลเรียบร้อยแล้ว");
                setEmpID(emp.getEmpId());
                  return "success";
            }else{
                con.rollback();
                setMessage("เกิดข้อผิดพลาด ไม่สามารถแก้ไขข้อมูลได้");
                  return "refill";
            }
        }else{
            con.rollback();
            setMessage("เกิดข้อผิดพลาด ไม่สามารถแก้ไขข้อมูลได้");
            return "refill";
        }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        
        
       
    }
    
    public String EmpByDeptPage() {
        
        try {
            
            DepartmentDAO deptDao = DAOFactory.createDepartmentDao(); 
            setListDepartment(deptDao.getAll());
           return SUCCESS; 
        } catch (Exception e) {
            e.printStackTrace();
            setMessage(e.getMessage());
            return ERROR;
        }
        
        
    }
    
    
    public String findEmpByDeptID(){
        try {
            
            EmployeeDAO empDao = DAOFactory.createEmployeeDao();
            
            emps = empDao.getByFilter("deptId", searchDept, "eq");
            System.out.println(emps.size());
            DepartmentDAO deptDao = DAOFactory.createDepartmentDao(); 
            setListDepartment(deptDao.getAll());
            
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            setMessage(e.getMessage());
            return ERROR;
        }
    }

    public String findEmpByID() throws Exception {
//        if (session.get("selectedemp") != null) {
//            session.remove("selectedemp");
//        }
      
        
        if (empID == null) {
            setEmpID(session.get("session_emp_id").toString());
        }else{
            session.put("session_emp_id", empID);
        }
        
        EmployeeDAO dao = DAOFactory.createEmployeeDao();
        
       setEmp(dao.getById(empID));
       session.put("selectedemp", emp);
       
        ReligionDAO rgDao = DAOFactory.createReligionDao();
        TitleDAO ttDao = DAOFactory.createTitleDao();
        SexTypeDAO stDao = DAOFactory.createSexTypeDao();
        BloodTypeDAO btDao = DAOFactory.createBloodTypeDao();
        ProvinceDAO pDao = DAOFactory.createProvincceDao();
        MaritalDAO mDao = DAOFactory.createMaritalDao();
        GraduationDAO gDao = DAOFactory.createGraduationDao();
        DepartmentDAO deptDao = DAOFactory.createDepartmentDao();
        EmpTypeDAO empTypeDao = DAOFactory.createEmpTypeDao();
        AddressDAO addDao = DAOFactory.createAddressDao();
        
        
         setAdd(addDao.getByEmpId(emp.getEmpId()));
        
         if (add.getProvinceID() != null) {
             setAddProvince(pDao.getById(add.getProvinceID()));
        }
        
         if (emp.getReligionID() != null) {
             setReligion(rgDao.getById(emp.getReligionID())); 
         }
         if (emp.getTitleNameID() != null) {
             setTitleName(ttDao.getById(emp.getTitleNameID()));
        }
         if (emp.getSexID() != null) {
             System.out.println("sex:" + emp.getSexID());
             setSexType(stDao.getById(emp.getSexID()));
        }
         
         if (emp.getBirthProvince() != null) {
             setBirthProvince(pDao.getById(emp.getBirthProvince()));
        }
         if (emp.getBloodTypeID()!= null) {
             setBloodType(btDao.getById(emp.getBloodTypeID()));
        }
         if (emp.getMaritalStatusID() != null) {
             setMarital(mDao.getById(emp.getMaritalStatusID()));
        }
         
         if (emp.getGraduateLevelID() != null) {
             setGraduate(gDao.getById(emp.getGraduateLevelID()));
        }
         if (emp.getDepartmentID() != null) {
             setDept(deptDao.getById(emp.getDepartmentID()));
        }
         if (emp.getEmpTypeID() != null) {
             setEmpType( empTypeDao.getById(emp.getEmpTypeID()));
        }
         
         
         
        System.out.println("religion :" +religion.getReligionName());
        
        
        
        return "success";
    }
    
    public String addEmpAndAddress() throws Exception {
        try {
            add.setAddressName("ที่อยู่ปัจจุบัน");
            add.setEmpID(emp.getEmpId());
            
            Calendar cal = Calendar.getInstance(Locale.US);
            
            
            if (emp.getBirthDate()!= null) {
               cal.setTime(emp.getBirthDate());
               cal.add(Calendar.YEAR, 543);
               emp.setBirthDate(cal.getTime());
               emp.setBirthDate(new java.sql.Date(emp.getBirthDate().getTime()));    
            }
            if (emp.getIdcardEndDate() != null) {
                cal.setTime(emp.getIdcardEndDate());
                cal.add(Calendar.YEAR, 543);
                emp.setIdcardEndDate(new java.sql.Date(cal.getTime().getTime()));
            }
            if (emp.getIdcardStartDate() != null) {
                cal.setTime(emp.getIdcardStartDate());
                cal.add(Calendar.YEAR, 543);
                emp.setIdcardStartDate(new java.sql.Date(cal.getTime().getTime()));
            }
            if (emp.getWorkStartDate() != null) {
                cal.setTime(emp.getWorkStartDate());
                cal.add(Calendar.YEAR, 543);
                emp.setWorkStartDate(new java.sql.Date(cal.getTimeInMillis()));
            }
            
             if (emp.getWorkEndDate()!= null) {
                cal.setTime(emp.getWorkEndDate());
                cal.add(Calendar.YEAR, 543);
                emp.setWorkEndDate(new java.sql.Date(cal.getTimeInMillis()));
            }
            
            
            if (emp.getWorkRegisDate() != null) {
                cal.setTime(emp.getWorkRegisDate());
                cal.add(Calendar.YEAR, 543);
                emp.setWorkRegisDate(new java.sql.Date(cal.getTime().getTime()));
            }
            
             System.out.println("empID :" + emp.getEmpId());
             Database con = DAOFactory.creatConnection();
        con.beginTransaction();
        EmployeeDAO empDao = DAOFactory.createEmployeeDao();
        empDao.setCon(con);
        int i = empDao.insertSingle(emp);
        System.out.println("add emp :" + i);
        if (i == 1) {
            AddressDAO addDao = DAOFactory.createAddressDao();
            
            addDao.setCon(con);
            i = addDao.insertSingle(add);
            System.out.println("add add :" + i);
            if (i == 1) {
                con.commit();
                setMessage("เพิ่มข้อมูลเรียบร้อยแล้ว");
                setEmpID(emp.getEmpId());
                  return "success";
            }else{
                con.rollback();
                setMessage("เกิดข้อผิดพลาด ไม่สามารถเพิ่มข้อมูลได้");
                  return "refill";
            }
        }else{
            con.rollback();
            setMessage("เกิดข้อผิดพลาด ไม่สามารถเพิ่มข้อมูลได้");
            return "refill";
        }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
      
        
    }
    
    public String formEditEmp() throws Exception {
       
         TitleDAO ttDao = DAOFactory.createTitleDao();
        SexTypeDAO stDao = DAOFactory.createSexTypeDao();
        ReligionDAO rgDao = DAOFactory.createReligionDao();
        BloodTypeDAO btDao = DAOFactory.createBloodTypeDao();
        ProvinceDAO pDao = DAOFactory.createProvincceDao();
        MaritalDAO mDao = DAOFactory.createMaritalDao();
        GraduationDAO gDao = DAOFactory.createGraduationDao();
        DepartmentDAO deptDao = DAOFactory.createDepartmentDao();
        EmpTypeDAO empTypeDao = DAOFactory.createEmpTypeDao();
        
        setListEmpType(empTypeDao.getAll());
        setListDepartment(deptDao.getAll());
        setListGraduation(gDao.getAll());
        setListMarital(mDao.getAll());
        setListProvince(pDao.getAll());
        setListBloodType(btDao.getAll());
        setListSexType(stDao.getAll());
        setListTitle(ttDao.getAll());
        setListReligion(rgDao.getAll());
        
        EmployeeDAO eDao = DAOFactory.createEmployeeDao();
        AddressDAO aDao = DAOFactory.createAddressDao();
        emp = eDao.getById(empID);
        add = aDao.getByEmpId(empID);
        
        System.out.println(emp);
        System.out.println(add);
        
        return SUCCESS;
        
       
    }
    
    
    
    public String formAddEmp() throws Exception {
        
        TitleDAO ttDao = DAOFactory.createTitleDao();
        SexTypeDAO stDao = DAOFactory.createSexTypeDao();
        ReligionDAO rgDao = DAOFactory.createReligionDao();
        BloodTypeDAO btDao = DAOFactory.createBloodTypeDao();
        ProvinceDAO pDao = DAOFactory.createProvincceDao();
        MaritalDAO mDao = DAOFactory.createMaritalDao();
        GraduationDAO gDao = DAOFactory.createGraduationDao();
        DepartmentDAO deptDao = DAOFactory.createDepartmentDao();
        EmpTypeDAO empTypeDao = DAOFactory.createEmpTypeDao();
        
        setListEmpType(empTypeDao.getAll());
        setListDepartment(deptDao.getAll());
        setListGraduation(gDao.getAll());
        setListMarital(mDao.getAll());
        setListProvince(pDao.getAll());
        setListBloodType(btDao.getAll());
        setListSexType(stDao.getAll());
        setListTitle(ttDao.getAll());
        setListReligion(rgDao.getAll());
        
        return "success";
    }
    
    
    
    public String execute() throws Exception {
        return "success";
    }

    public List<Employee> getEmps() {
        return emps;
    }

    public void setEmps(List<Employee> emps) {
        this.emps = emps;
    }

    public String getSearchDept() {
        return searchDept;
    }

    public void setSearchDept(String searchDept) {
        this.searchDept = searchDept;
    }

    
    
    public String getOldEmpId() {
        return oldEmpId;
    }

    public void setOldEmpId(String oldEmpId) {
        this.oldEmpId = oldEmpId;
    }
    
    

    public Province getAddProvince() {
        return addProvince;
    }

    public void setAddProvince(Province addProvince) {
        this.addProvince = addProvince;
    }

    
    
    public Address getAdd() {
        return add;
    }

    public void setAdd(Address add) {
        this.add = add;
    }

    
    
    public SexType getSexType() {
        return sexType;
    }

    public void setSexType(SexType sexType) {
        this.sexType = sexType;
    }

    public Province getBirthProvince() {
        return birthProvince;
    }

    public void setBirthProvince(Province birthProvince) {
        this.birthProvince = birthProvince;
    }

    public BloodType getBloodType() {
        return bloodType;
    }

    public void setBloodType(BloodType bloodType) {
        this.bloodType = bloodType;
    }

    public MaritalStatus getMarital() {
        return marital;
    }

    public void setMarital(MaritalStatus marital) {
        this.marital = marital;
    }

    public Graduation getGraduate() {
        return graduate;
    }

    public void setGraduate(Graduation graduate) {
        this.graduate = graduate;
    }

    public Department getDept() {
        return dept;
    }

    public void setDept(Department dept) {
        this.dept = dept;
    }

    public EmployeeType getEmpType() {
        return empType;
    }

    public void setEmpType(EmployeeType empType) {
        this.empType = empType;
    }

    
    
    public TitleName getTitleName() {
        return titleName;
    }

    public void setTitleName(TitleName titleName) {
        this.titleName = titleName;
    }

    
    
    public Religion getReligion() {
        return religion;
    }

    public void setReligion(Religion religion) {
        this.religion = religion;
    }

    
    
    public Employee getEmp() {
        return emp;
    }

    public void setEmp(Employee emp) {
        this.emp = emp;
    }
    
    

    public String getEmpID() {
        return empID;
    }

    public void setEmpID(String empID) {
        this.empID = empID;
    }

    
    
    public List<TitleName> getListTitle() {
        return listTitle;
    }

    public void setListTitle(List<TitleName> listTitle) {
        this.listTitle = listTitle;
    }

    public List<SexType> getListSexType() {
        return listSexType;
    }

    public void setListSexType(List<SexType> listSexType) {
        this.listSexType = listSexType;
    }

    public List<Religion> getListReligion() {
        return listReligion;
    }

    public void setListReligion(List<Religion> listReligion) {
        this.listReligion = listReligion;
    }

    public List<BloodType> getListBloodType() {
        return listBloodType;
    }

    public void setListBloodType(List<BloodType> listBloodType) {
        this.listBloodType = listBloodType;
    }

    public List<Province> getListProvince() {
        return listProvince;
    }

    public void setListProvince(List<Province> listProvince) {
        this.listProvince = listProvince;
    }

    public List<MaritalStatus> getListMarital() {
        return listMarital;
    }

    public void setListMarital(List<MaritalStatus> listMarital) {
        this.listMarital = listMarital;
    }

    public List<Graduation> getListGraduation() {
        return listGraduation;
    }

    public void setListGraduation(List<Graduation> listGraduation) {
        this.listGraduation = listGraduation;
    }

    public List<Department> getListDepartment() {
        return listDepartment;
    }

    public void setListDepartment(List<Department> listDepartment) {
        this.listDepartment = listDepartment;
    }

    public List<EmployeeType> getListEmpType() {
        return listEmpType;
    }

    public void setListEmpType(List<EmployeeType> listEmpType) {
        this.listEmpType = listEmpType;
    }
    
    
    
    
    
    public Map<String, Object> getSession() {
        return session;
    }
  
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }

    public Integer getActiveTab() {
        return activeTab;
    }

    public void setActiveTab(Integer activeTab) {
        this.activeTab = activeTab;
    }
    
    
    
}
